<?php
/**
 * @version     1.0.0
 * @package     com_glycophile
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Ali SAHLI <sahli28@gmail.com> - https://plus.google.com/u/0/115470606844807318192/posts
 */

defined('_JEXEC') or die;

jimport( 'joomla.filesystem.file' );

class GlycophileHelper
{
	function listRaffineries($idPays){

    $db = JFactory::getDbo();
		$query = $db->getQuery(true);
    $query->select('*');
    $query->from('`#__glycophile_raffinerie` AS a');
    $query->select('p.pays AS pays');
    $query->join('LEFT', '#__glycophile_pays AS p ON p.identpays=a.identpays');
    $query->where('a.identpays = '.(int) $idPays);
    $query->where('a.state = 1');
    $query->order('raffinerie');


    $db->setQuery($query);
    $raffineries = $db->loadObjectList();

   return $raffineries;
  }

  function listPaysEtrangeres(){

    $db = JFactory::getDbo();
    $query = "SELECT pays, identPays, nombreserie, affichetheme
              FROM #__glycophile_pays WHERE identPays <> 1
              AND nombreserie > 0 AND state = 1 ORDER BY pays";
    $db->setQuery($query);
    $pays = $db->loadObjectList();
    
    return $pays;
  }
  
  function getThemesById($theme_id = 0){
	if(!$theme_id) return array();
	$db = JFactory::getDbo();
    $query = "SELECT * FROM #__glycophile_theme WHERE id=".$theme_id;
    $db->setQuery($query);
    $theme = $db->loadObject();
    
    return $theme;
  }
  
  function getSerieById($serie_id = 0){
	if(!$serie_id) return array();
	$db = JFactory::getDbo();
    $query = "SELECT * FROM #__glycophile_serie WHERE id=".$serie_id;
    $db->setQuery($query);
    $serie = $db->loadObject();
    return $serie;
  }
  
  function getSubSeriesBySerieId($serie_id = 0){
	if(!$serie_id) return array();
	$db = JFactory::getDbo();
    $query = "Select * From #__glycophile_sousserie ss";
	$query .= "	Where id_serie = ".$serie_id;
	$db->setQuery($query);
	$sseries = $db->loadObjectList();
    
    return $sseries;
  }
  
	function getPaginationBar($NumRows, $lignesByPage, $link, $params = 'limitstart' ){
	
		$tmpl = JRequest::getVar('tmpl', '');
		if($tmpl == 'component'){
			$link .= '&tmpl=component' ;
		}
		
		$_html = '';
		$NbrPages = ceil($NumRows/$lignesByPage);
		if($NbrPages <= 1) return $_html ;
		$currentPage = JRequest::getVar($params, 1);
		$prevPage = $currentPage - 1;
		$nextPage = $currentPage + 1;
		
		$_html .= "<div class='pagination' style='height:auto;'>";
		$_html .= '<div class="counter">Page '. $currentPage .' / '. $NbrPages .'</div>';
		$_html .="<ul>";
		////////////////////////////////////////////////////
		if($currentPage > 1) 
			$_html .="<li class='pagination-prev'><a href='$link&$params=$prevPage' class='pagenav'>Précédent</a> </li>";
		else
			$_html .="<li class='pagination-prev'><span class='pagenav'>Précédent</span></li>";
		////////////////////////////////////////////////////
		if($NbrPages > 11){
			$start = $currentPage > 6 ? $currentPage - 5 : 1 ;
			$end = $currentPage < ($NbrPages - 5) ? $start + 9 : $NbrPages  ;
		}else{
			$start = 1;
			$end = $NbrPages;
		}
		for($i=$start ; $i<=$end ; $i++){ 
			if($currentPage == $i)
				$_html .= "<li><span class='pagenav' >$i</span></li>";
			else
				$_html .= "<li><a class='pagenav' href=$link&$params=$i>$i</a></li>";
		}
		////////////////////////////////////////////////////
		if($currentPage < $NbrPages) 
			$_html .="<li class='pagination-next'><a href='$link&$params=$nextPage' class='pagenav'>Suivant</a> </li>";
		else
			$_html .="<li class='pagination-next'><span class='pagenav'>Suivant</span> </li>";
		////////////////////////////////////////////////////			
		$_html .="</ul>";
		$_html .="</div>";
		
		return $_html;	
	}


  function getImage($id_sucre){

    $imageName = "";

    $q = "SELECT *
			FROM `#__glycophile_inventaire`
			WHERE `id_sucre` =". $sucre->id ;
			$db->setQuery($query);

    $query = "SELECT concat(s.id_pays, '/', t.code,'/', s.id, '/', i.id , '.jpg') URL
						  FROM `#__glycophile_pays` p, #__glycophile_theme t, #__glycophile_serie s,
			              #__glycophile_sousserie ss, #__glycophile_inventaire i
						        WHERE t.identpays= s.id_pays
			              AND p.identpays = s.id_pays AND t.id= s.id_theme
			              AND ss.id_serie=s.id and i.id_sous_serie = ss.id
			              AND i.TailleX > 0 AND i.TailleY > 0
			              AND i.id=$id_invetaire
			              ORDER by p.identpays, t.code, s.id
              ";

        $db = JFactory::getDbo();
				$db->setQuery($query);
				$imageName = $db->loadResult();
        if($imageName != ""){
          $imageName = PX_COMPATHIMAGE.$imageName;
        } else {
          $imageName = PX_COMPATHIMAGE."noimage.jpg";
        }

        return $imageName;

  }


  function getSucresByNominatif($designation){

    $db = JFactory::getDbo();

    $type = JRequest::getVar('type', 0);
    if($type == 0){
      $id_pays = JRequest::getVar('pays', 0);
    } else {
      $id_pays = 1;

    }
    $designation = urldecode(base64_decode($designation));

   // echo "designation : ".$designation;
    $query = "SELECT n.*, t.Tome, t.Code as NumCat, n.rang as rang, r.Raffinerie, c.Classement, f.Code as CodeFam, f.Famille";
    $query .= " FROM #__glycophile_nominatif AS n, #__glycophile_tome as t, #__glycophile_raffinerie AS r, #__glycophile_classement as c, #__glycophile_type as ty, #__glycophile_famille as f";
    $query .= ' WHERE n.Designation="'.$designation.'"';
    $query .= " AND n.IdentTome = t.id AND n.IdentRaffinerie = r.id";
    if($id_pays == 1){
      $query .= " AND t.typeSucre <> 'E'";
      $query .= ' AND n.IdentPays ='. $id_pays;
    } else {
      $query .= " AND t.typeSucre = 'E'";
    }
    
    $query .= " AND n.IdentClassement = c.id AND n.IdentType = ty.id AND ty.identfamille = f.id";
    $query .= " ORDER  BY t.rang, t.code, Ordre";

//    echo "<br />";
 //  echo $query;
    $db->setQuery($query);
    $series = $db->loadObjectList();

    return $series;
  }	
	
	function fileExists($file){
		return JFile::exists($file);
	}
	
	function preAffiche_Image3($ima, $taille_maxi, $texte_inex = '', $modvisu = "V") {
		$pos = strrpos($ima, "/");
		$rima = substr_replace($ima, 'R', $pos+1, 0);
		@$size = getimagesize($ima);
		$X = $size[0] ;
		$Y = $size[1] ;
		if ($X > $Y) $Y = $X;
		
		$res['imgPath'] = $ima ;
		$res['rimgPath'] = $rima ;
		$res['img'] = $this->Affiche_Image3($ima, $taille_maxi, $modvisu, $texte_inex);
		if(JFile::exists($rima)){ $res['rimg'] = $this->Affiche_Image3($rima, $taille_maxi, $modvisu, $texte_inex); }
		return $res ;
	}
  
	function Affiche_Image3 ($img, $taille_maxi, $modvisu = "V", $texte_inex= '') {
		$HTML = '';
		//
  //  echo $img;
		if ($modvisu == 'P') {
			$borderimg = "1";
		} else {
			$borderimg = "none";
		}
		if (JFile::exists($img)) {
			$size = getimagesize($img);
			$Xdetail = $size[0];
			$Ydetail = $size[1];

			// Image retaillée pour tenir dans le tableau
			$X = $Xdetail;
			$Y = $Ydetail;
					
			if ($taille_maxi > 1) {
				if ($X > $taille_maxi) {
					$Y = intval($Y * ($taille_maxi / $X));
					$X = $taille_maxi;
				}
				if ($Y > $taille_maxi) {
					$X = intval($X  * ($taille_maxi / $Y));
					$Y = $taille_maxi;
				}
			} else {
					$X = intval($X * $taille_maxi);
					$Y = intval($Y * $taille_maxi);
			}
			
			
			//if ($X > $Y) {
			if(1){
				$HTML = "<img src='". JURI::root() ."$img' width='$X' height='$Y' border='$borderimg' alt='$texte_inex' />";
				//return true;
			} else {
				return false;
			}
		} else { 
			$HTML = "<em><font color='#9999FF' size='-1'> Photo &agrave; venir</font></em>\n";
		}
		return $HTML;
	}

}

